package com.ctshk.rpc.train.mapper;

import com.ctshk.common.service.mapper.CtsBaseMapper;
import com.ctshk.common.utils.Assist;
import com.ctshk.rpc.train.dto.TrainProductCostDTO;
import com.ctshk.rpc.train.dto.TrainProductCostPriceDTO;
import com.ctshk.rpc.train.entity.TrainProductCost;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 火车产品成本表 Mapper 接口
 * </p>
 *
 * @author 彭柳霖
 * @since 2021-03-11
 */
public interface TrainProductCostMapper extends CtsBaseMapper<TrainProductCost> {

    /**
     * 根据产品id查询产品成本集合
     * @param trainProductId
     * @return
     */
    @Select("<script>"+"select id,bus_product_id,cost_name,currency_id,currency_name from t_train_product_cost \n" +
            "where train_product_id = #{trainProductId} \n" +
            "</script>")
    List<TrainProductCostDTO> queryList(@Param("trainProductId") Long trainProductId);




    /**
     * 查询产品成本价格,币种
     */
    @Select("<script>"+"select e.passenger_id passengerId,e.price price,a.currency_id currencyId,a.currency_name currency from t_train_product_cost a " +
            "left join t_train_product b on a.train_product_id  = b.id \n" +
            "left join t_train_product_cost_date c on a.id = c.cost_id \n" +
            "left join t_train_product_cost_price e on a.id = e.cost_id \n" +
            "WHERE b.is_deleted = 0 \n" +
            "AND c.date_begin &lt;=#{queryDate} and c.date_end &gt;=#{queryDate}\n" +
            "AND b.id =#{productId}\n" +
            "</script>")
    List<TrainProductCostPriceDTO> queryPrice(@Param("productId") Long productId, @Param("queryDate") LocalDate queryDate);

    /**
     * 查询产品成本价格列表
     * @param productId
     * @param queryDate
     * @return key：旅客类型；value：价格
     */
    default Map<Long, BigDecimal> queryPriceMap(Long productId, LocalDate queryDate) {
        List<TrainProductCostPriceDTO> list = queryPrice(productId, queryDate);
        return Assist.forEachToMap(list, TrainProductCostPriceDTO::getPassengerId, TrainProductCostPriceDTO::getPrice);
    }
}
